define([], function() {
    'use strict';
    /**
     * 树形插件显示
     */
    function Tree(options){
        if(!options.id){
            throw new Error('this is no container unique id');
        }
        this.id = options.id;
        this.box = $("#" + options.id);
        this.data = options.data || [];
        this.key = options.key || 'value';
        this.showKey = options.showKey || 'name' ;
        this.children = options.children || 'children';
        this.init();
    }

    Tree.prototype.init = function(){
        this.renderHtml();
    }

    Tree.prototype.groupListStr = function(list, level){
        var that = this;
        var str = '<div class="tree-item-list" data-level="'+ level +'">';
        list.forEach(function(item){
            var showValue = item[that.showKey], keyValue = item[that.key], aList = item[that.children];
            var openIcon = aList && aList.length > 0 ? 'close' : "open";
            str += '<div class="item-name '+ openIcon +'" data-level="'+ level +'" key="'+ keyValue +'">'+ showValue +'</div>';
            if(aList && aList.length > 0){
                str += that.groupListStr(aList, level + 1);
            }
        })
        str += '</div>';
        return str;
    }

    Tree.prototype.renderHtml = function(){
        var str = '<div class="yby-tree-box">';
        str += this.groupListStr(this.data, 0);
        str += '<div>';
        this.box.html(str);
    }

    return Tree;
});